<?php

namespace app\common\model\user;

use app\common\model\shop\Order;
use app\common\model\User;
use think\Model;


class Alloca extends Model
{





    // 表名
    protected $name = 'alloca';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_claim_text',
        'is_effective'
    ];


    public function getIsEffectiveAttr($v, $d)
    {
        $total_fee = Order::where('user_id', $d['user_id'])->where('status', '>', 0)->sum('total_fee') ?? 0;
        return $total_fee >= 2000 ? true : false;
    }

    public function getIsClaimList()
    {
        return ['0' => __('Is_claim 0'), '1' => __('Is_claim 1')];
    }
    public function orders()
    {
        return $this->hasMany(Order::class, 'user_id', 'user_id');
    }

    public function getIsClaimTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_claim']) ? $data['is_claim'] : '');
        $list = $this->getIsClaimList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function pool()
    {
        return $this->belongsTo(Pool::class, 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
